package models.venue.curriculum;

import models.BaseModel;
import models.constants.DeletedStatus;
import models.weixin.WebUser;
import order.Order;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Created by youliangcheng on 17/6/1.
 */
@Entity
@Table(name = "webUser_curriculums")
public class WebUserCurriculum extends BaseModel {
    /**
     * 用户
     */
    @JoinColumn(name = "web_user_id", nullable = true)
    @ManyToOne
    public WebUser webUser;

    /**
     * 课程
     */
    @JoinColumn(name = "curriculum_id", nullable = true)
    @ManyToOne
    public Curriculum curriculum;

    /**
     * 课程开始时间
     */
    @Column(name="start_time", length = 50)
    public Date startTime;

    /**
     * 课程结束时间
     */
    @Column(name="end_time", length = 50)
    public Date endTime;

    /**
     * 总课程数量
     */
    @Column(name="quantity")
    public Integer quantity;

    /**
     * 已上课程数量
     */
    @Column(name="be_used_num")
    public Integer beUsedNum = 0;

    /**
     * 剩余课程数量
     */
    @Column(name="residue_num")
    public Integer residueNum;


    /**
     * 购买订单
     */
    @JoinColumn(name = "order_id", nullable = true)
    @ManyToOne
    public Order order;


    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;


    /**
     * 根据用户查询课程
     * @param userId
     * @return
     */
    public static List<WebUserCurriculum> loadByUserId(long userId){
        return WebUserCurriculum.find("webUser.id = ? and deleted = ?" ,userId , DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public static WebUserCurriculum findByWebUserCurriculumId(long id){
        return  WebUserCurriculum.find("id = ? and deleted = ? " ,id , DeletedStatus.UN_DELETED).first();
    }













}
